<?php

namespace app\api\service;

use think\Controller;
use think\Db;
use Des3\DES3;
use think\Exception;
use think\Loader;

class Wallet extends Controller {

    /**
     * 获取交易明细
     * @param $param
     * @return array
     */
   public static function get_member_bill($param) {
       try {
           $member_bill = model('member_bill')->where(['member_id' => $param['member_id']])->order('id desc')->paginate();
           $data = [];
           $type_array = ['', '订单支付', '充值', '订单分成', '提现', '保证金返还'];
           $withdraw_status = ['', '已申请', '已审核', '审核失败'];
           foreach ($member_bill as $v) {
               if ($v->type == 1 || $v->type == 4) {
                   $money = '-' . $v->money;
               } else {
                   $money = '+' . $v->money;
               }
               $data[] = [
                   'money' => $money,
                   'type' => $type_array[$v->type],
                   'create_time' => $v->create_time,
                   'withdraw_status' => $withdraw_status[$v->type_4_status],
                   'id' => $v->id
               ];
           }

           return api_return(200, $data);
       } catch (\Exception $e) {
           return api_return(0, [], $e->getMessage());
       }
   }

    /**
     * 获取交易明细详情
     * @param $param
     * @return array
     */
   public static function get_member_bill_info($param) {
       try {
           $member_bill = model('member_bill')->get(['member_id' => $param['member_id'], 'id' => $param['id']]);
           if ($member_bill->type == 1 || $member_bill->type == 4) {
               $money = '-' . $member_bill->money;
           } else {
               $money = '+' . $member_bill->money;
           }
           $type_array = ['', '订单支付', '充值', '订单分成', '提现', '保证金返还'];
           $withdraw_status = ['', '已申请', '已审核', '审核失败'];
           $products = [];
           $shop_logo = '';
           if ($member_bill->type == 1 && $member_bill->order_model == 'order') {
               $order = model('order')->get(['order_sn' => $member_bill->order_sn]);
               if ($order->shop_id > 0) {
                   $shop_logo = get_img_url($order->shop->logo);
               }

               $order_product = model('order_product')->all(['order_sn' => $member_bill->order_sn]);
               foreach ($order_product as $v) {
                   $products[] = [
                       'name' => $v->name,
                       'num' => $v->num,
                   ];
               }
           }
           $data = [
               'order_sn' => $member_bill->order_sn,
               'create_time' => $member_bill->create_time,
               'money' => $money,
               'type' => $type_array[$member_bill->type],
               'withdraw_status' => $withdraw_status[$member_bill->type_4_status],
               'pay_type' => $member_bill->pay_type,
               'msg' => $member_bill->msg,
               'shop_logo' => $shop_logo,
               'products' => $products,
           ];

           return api_return(200, $data);
       } catch (\Exception $e) {
           return api_return(0, [], $e->getMessage());
       }
   }

    /**
     * 保存银行卡
     * @param $param
     * @return array
     */
   public static function save_bank($param) {
       try {
           $order_data = [
               'member_id' => $param['member_id'],
               //'member_id' => 0,
               'proposer' => $param['proposer'],
               'bank_card' => $param['bank_card'],
               'mobile' => $param['mobile']
           ];

           return api_return(200, []);
       } catch (\Exception $e) {
           return api_return(0, [], $e->getMessage());
       }
   }

    /**
     * 保存用户提现申请
     * @param $param
     * @return array
     */
   public static function withdraw_apply_for($param) {
       try {
           $member = model('member')->get(['id' => $param['member_id']]);
           if ($param['money'] > $member['money']) {
               throw new \Exception('账户余额不足！');
           }
           $withdraw_sn = 'TX' . date('YmdHis') . rand(1000, 9999);
           $data = [
               'member_id' => $param['member_id'],
               'member_bank_id' => $param['member_bank_id'],
               'money' => $param['money'],
               'withdraw_sn' => $withdraw_sn
           ];
           model('member_withdraw')->save($data);

           // 提现申请成功 更新账户余额
           model('member')->where(['id' => $param['member_id']])->setDec('money', $param['money']);
           MemberBill::add_member_bill($param['member_id'], $param['money'], '提现', 4, $withdraw_sn, 'member_withdraw',  '提现');

//           // 短信通知
//           $param = [
//               'mobile' => config('system.enter_apply_for_inform_number'),
//               'order_sn' => $withdraw_sn,
//               'type' => 12,
//               'price' => $param['money'],
//               'username' => $member->username,
//           ];
//           NoteCode::send_code($param);

           return api_return(200, []);
       } catch (\Exception $e) {
           return api_return(0, [], $e->getMessage());
       }
   }
}